﻿using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using Module;
using Module.QuanLyDuoc.HeThongNhapXuat;

namespace HospitalManager.Webservice.QuanLyDuoc.HeThongNhapXuat
{
    /// <summary>
    /// Summary description for wsPhieuHuy
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [ScriptService]
    public class wsPhieuHuy : WebService
    {
        private readonly PhieuHuy _phieuHuy = new PhieuHuy();

        [WebMethod(EnableSession = true)]
        public string PhieuHuy_Insert(string makho, string ngayhuy, string nguoihuy, 
            string nguoigiamsat, string diengiai, string nguoitao)
        {
            var obj = new PhieuHuy
            {
                MaKho = makho,
                NgayHuy = Convert.ToDateTime(DateTime.ParseExact(ngayhuy, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiHuy = nguoihuy,
                NguoiGiamSat = nguoigiamsat,
                DienGiai = diengiai,
                NguoiTao = nguoitao
            };
            return _phieuHuy.PhieuHuy_Insert(obj) ? obj.MaPhieu : string.Empty;
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuHuy_Update(string makho, string maphieu, string ngayhuy, string nguoihuy, 
            string nguoigiamsat, string diengiai, string nguoitao)
        {
            var obj = new PhieuHuy
            {
                MaKho = makho,
                MaPhieu = maphieu,
                NgayHuy = Convert.ToDateTime(DateTime.ParseExact(ngayhuy, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiHuy = nguoihuy,
                NguoiGiamSat = nguoigiamsat,
                DienGiai = diengiai,
                NguoiTao = nguoitao
            };
            return _phieuHuy.PhieuHuy_Update(obj);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuHuy_SelectById(string maphieu)
        {
            return JsonMethods.ToJsonString(_phieuHuy.PhieuHuy_SelectById(maphieu));
        }

        [WebMethod(EnableSession = true)]
        public string PhieuHuy_VatTuByMaKho(string makho)
        {
            var data = _phieuHuy.PhieuHuy_VatTuByMaKho(makho);
            if (data.Rows.Count <= 0) return null;
            var list =
                data.AsEnumerable()
                    .Select(x => new { MaVatTu = x.Field<string>("MaVatTu"), TenVatTu = x.Field<string>("TenVatTu") })
                    .Distinct()
                    .ToList();
            return (new JavaScriptSerializer { MaxJsonLength = int.MaxValue }).Serialize(list);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuHuy_LoSanXuat_ByMaVatTu(string makho, string mavattu)
        {
            var data = _phieuHuy.PhieuHuy_VatTuByMaKho(makho);
            if (data.Rows.Count <= 0) return null;
            var list =
                data.AsEnumerable()
                    .Where(x => x.Field<string>("MaVatTu").Equals(mavattu))
                    .Select(
                        x => new { MaLoSanXuat = x.Field<string>("LoSanXuat"), LoSanXuat = x.Field<string>("LoSanXuat") })
                    .Distinct()
                    .ToList();
            return (new JavaScriptSerializer { MaxJsonLength = int.MaxValue }).Serialize(list);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuHuy_MaPhieuNhap_By_LoSanXuat(string makho, string mavattu, string losanxuat)
        {
            var data = _phieuHuy.PhieuHuy_VatTuByMaKho(makho);
            if (data.Rows.Count <= 0) return null;
            var list =
                data.AsEnumerable()
                    .Where(row => row.Field<string>("MaVatTu") == mavattu && row.Field<string>("LoSanXuat") == losanxuat)
                    .Select(
                        x =>
                            new
                            {
                                PhieuNhap = x.Field<string>("MaPhieuNhap"),
                                MaPhieuNhap = x.Field<string>("MaPhieuNhap")
                            }).Distinct().ToList();
            return (new JavaScriptSerializer { MaxJsonLength = int.MaxValue }).Serialize(list);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuHuy_DonGia_ByMaPhieuNhap(string makho, string mavattu, string losanxuat, string maphieunhap)
        {
            var data = _phieuHuy.PhieuHuy_VatTuByMaKho(makho);
            if (data.Rows.Count <= 0) return null;
            var list =
                data.AsEnumerable()
                    .Where(
                        row => row.Field<string>("MaVatTu") == mavattu && row.Field<string>("LoSanXuat") == losanxuat &&
                               row.Field<string>("MaPhieuNhap") == maphieunhap)
                    .Select(x => new { DonGia = x.Field<double>("DonGia"), HanSuDung = x.Field<DateTime>("HanSuDung") })
                    .ToList();
            return (new JavaScriptSerializer { MaxJsonLength = int.MaxValue }).Serialize(list);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuHuy_ChiTiet_Insert(string maphieu, string mavattu, string madonvi, int soluong, double dongia,
            string losanxuat, string hansudung, string maphieunhap, string nguoitao)
        {
            var obj = new PhieuHuy
            {
                MaPhieu = maphieu,
                MaVatTu = mavattu,
                MaDonVi = madonvi,
                SoLuong = soluong,
                DonGia = dongia,
                LoSanXuat = losanxuat,
                HanSuDung = Convert.ToDateTime(DateTime.ParseExact(hansudung, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                MaPhieuNhap = maphieunhap,
                NguoiTao = nguoitao
            };
            return _phieuHuy.PhieuHuy_ChiTiet_Insert(obj);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuHuy_ChiTiet_Update(string id, string maphieu, string mavattu, string madonvi, int soluong, double dongia,
            string losanxuat, string hansudung, string maphieunhap, string nguoitao)
        {
            var obj = new PhieuHuy
            {
                ID = id,
                MaPhieu = maphieu,
                MaVatTu = mavattu,
                MaDonVi = madonvi,
                SoLuong = soluong,
                DonGia = dongia,
                LoSanXuat = losanxuat,
                HanSuDung = Convert.ToDateTime(DateTime.ParseExact(hansudung, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                MaPhieuNhap = maphieunhap,
                NguoiTao = nguoitao
            };
            return _phieuHuy.PhieuHuy_ChiTiet_Update(obj);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuHuy_ChiTiet_Delete(string id)
        {
            return _phieuHuy.PhieuHuy_ChiTiet_Delete(id);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuHuy_ChiTiet_SelectById(string id)
        {
            return JsonMethods.ToJsonString(_phieuHuy.PhieuHuy_ChiTiet_SelectById(id));
        }

        [WebMethod(EnableSession = true)]
        public string PhieuHuy_ChiTiet_SelectByMaPhieu(string maphieu)
        {
            return JsonMethods.ToJsonString(_phieuHuy.PhieuHuy_ChiTiet_SelectByMaPhieu(maphieu));
        }
    }
}
